﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace BuyLocalBonds
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // bind result of sql select to gridview
            DataSet ds = (DataSet) Session["resultDataSet"];
            SearchBondResultsGV.DataSource = ds.Tables[0];
            SearchBondResultsGV.DataBind();

            // if empty, ask them to search again
            if (ds.Tables[0].Rows.Count == 0)
            {
                MessageLabelEmpty.Text = "No Bonds found with the selected criteria. Search Again!";
                BuyButton.Enabled = false;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e)
        {

        }

        protected void SelectedIndexChanged(object sender, EventArgs e)
        {
            // Get the currently selected row using the SelectedRow property.
            GridViewRow row = SearchBondResultsGV.SelectedRow;

            // Display the company name from the selected row.
            // In this example, the third column (index 2) contains
            // the company name.
            MessageLabel.Text = "You selected the Bond " + row.Cells[2].Text + ".";
            MessageLabelEmpty.Text = "";

            // saving session variables
            Session["cusip"] = row.Cells[1].Text;
            Session["rating"] = row.Cells[3].Text;
            Session["current_yield"] = row.Cells[5].Text;
            Session["ytm"] = row.Cells[6].Text;
            Session["coupon"] = row.Cells[4].Text;
            Session["maturity_date"] = row.Cells[7].Text;
            Session["price"] = row.Cells[9].Text;
            Session["qty_avail"] = row.Cells[11].Text;
        }

        protected void BuyButton_Click(object sender, EventArgs e)
        {
            if (SearchBondResultsGV.SelectedRow == null)
            {
                MessageLabelEmpty.Text = "Select a Bond to Buy!";
            }
            else
            {
                Response.Redirect("Buy_Bond.aspx");
            }
        }

        protected void BackToSearchButton_Click(object sender, EventArgs e)
        {
            //Session.Contents.RemoveAll(); // but, keep trader login! CHANGE THIS
            Response.Redirect("searchBond.aspx");
        }
    }
}
